const puppeteer = require('puppeteer');
const { exec } = require('child_process');
const { resolve } = require("path");

(async function () {
    // 1. 创建一个浏览器
    const brower = await puppeteer.launch({ headless: false })
    // 2. 搞个页面
    const page = await brower.newPage()
    // 3. 跳转页面
    await page.setViewport({ width: 1920, height: 1080 })
    await page.goto('https://www.juejin.cn')

    // 5. 操作元素
    // $获取单个元素，$$获取多个元素
    const collectFn = async () => {
        const titleList = [];
        let pages = 1;
        let size = 0;
        const SELECTOR = ".entry-list .title-row a"
        await page.waitForSelector(`${SELECTOR}:nth-of-type(1)`, { timeout: 3000 });
        while (pages < 3) {
            await page.waitForFunction((size) => {
                const _size = document.querySelectorAll(".entry-list .title-row").length;
                return _size != size;
            }, { timeout: 3000 }, size) // 加载更多的数据拿到后元素的数量会变化，此时也表示加载更多完成，继续获取下一页
            size = (await page.$$(".entry-list .title-row a")).length;
            await page.evaluate(() => {
                window.scrollTo(0, document.body.scrollHeight); // 滚动到底部，会自动加载更多
            })
            pages++;
        }
        const elements = await page.$$(".entry-list .title-row a")
        for await (const el of elements) {
            // 获取到属性后还得调用下jsonValue()才能拿到里面的内容
            const prop = await el.getProperty("innerText");
            const text = await prop.jsonValue();
            titleList.push(text);
        }
        console.log(titleList.join(","));
        const pyProcess = exec(`python ${resolve(__dirname, 'index.py')} ${titleList.join(",")}`)
        pyProcess.stdout.on("data", (data) => {
            console.log("stdout：", data.toString("utf-8"));
        })
        pyProcess.stderr.on("data", (data) => {
            console.log("stderr：", data.toString("utf-8"));
        })
        pyProcess.on("close", (code) => {
            console.log(`子进程退出，退出码${code}`);
        })
    }
    // 4. 等待元素出现再操作
    await page.waitForSelector(".side-navigator-wrap")
    const elements = await page.$$(".side-navigator-wrap .nav-item-wrap span")
    // for...of替代for遍历数组同步解决
    for await (const el of elements) {
        // 获取到属性后还得调用下jsonValue()才能拿到里面的内容
        const prop = await el.getProperty("innerText");
        const text = await prop.jsonValue();
        if (text === "前端") {
            await el.click();

            await collectFn();
            break; //break跳出循环
        }
    }
})()
